<template>
  <div class="talk">
    <button @click="getLoveTalk">获取一句土味情话</button>
    <ul>
      <li v-for="item in talkStore.talkList" :key="item.id">
        {{ item.title }}
      </li>
    </ul>
    <h3>传入id获取名称：{{ talkStore.getUserById(2)?.name }}</h3>
    <h3>获取count的getter拼接字符串：{{ talkStore.countGetter }}</h3>
  </div>
</template>

<script lang="ts" setup name="LoveTalk">
import { storeToRefs } from "pinia";
import axios from "axios";
import { nanoid } from "nanoid";
import { useTalkStore } from "@/store/talk";

const talkStore = useTalkStore();
const { talkList } = storeToRefs(talkStore);
async function getLoveTalk() {
  // 发请求,连续结构赋值+重命名
  let {
    data: { content: title },
  } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");
  let obj = { id: nanoid(), title };
  talkList.value.unshift(obj);
}
</script>

<style lang="less" scoped>
.talk {
  background-color: pink;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px;
}
</style>
